const handleBlogRouter = require('./src/router/blog')
const handleUserRouter = require('./src/router/user')
const queryString = require('querystring')

const serverHandle = (req, res) => {
  res.setHeader('Content-type', 'application/json')
  // 获取path
  const url = req.url
  req.path = url.split('?')[0]

  // 解析query
  req.query = queryString.parse(url.split('?')[1])

  // 处理blog路由
  const blogData = handleBlogRouter(req, res)
  if(blogData) res.end(JSON.stringify(blogData))
  
  // 处理user路由
  const userData = handleUserRouter(req, res)
  if(userData) res.end(JSON.stringify(userData))
}

module.exports = serverHandle